package com.demo.hc.common.util;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.ResourceBundle;

/**
 * 配置资源
 */
public class ConfigResource {

	protected static final Logger logger = LogManager.getLogger();

	/**
	 * 配置文件
	 */
	public final static ResourceBundle config = ResourceBundle.getBundle("config");

	/**
	 * 获取系统属性，该方法主要被velocity模版所使用。
	 *
	 * @param key 键值
	 * @return
	 */
	public static String getSystemProperty(String key) {
		return config.getString(key);
	}

	private static Properties props = null;

	static{
		try {
			props = PropertiesLoaderUtils.loadProperties(new ClassPathResource("config.properties"));
		} catch (IOException e) {
			logger.warn(e.getMessage());
		}
	}

	public static String get(String key){
		return props.getProperty(key);
	}

	public static void set(String key, String value){
		props.setProperty(key, value);
		try{
			OutputStream fos = new FileOutputStream(ConfigResource.class.getResource("/config.properties").getFile().toString());
			props.store(fos, null);
		}catch (IOException e){
			logger.warn(e);
		}
	}

}
